package ifes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;

/*
 * Etiqueta as palavras de um texto.
 */
public class Tagger {    
    
    public static String[][] doTagger(String texto) {
        SentenceModel sd_model = null;
        POSTaggerME tagger = null;

        try {
            POSModel model = new POSModelLoader().load(new File("pt-pos-maxent.bin"));
            tagger = new POSTaggerME(model);
            sd_model = new SentenceModel(new FileInputStream("pt-sent.bin"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        SentenceDetectorME mSD = new SentenceDetectorME(sd_model);
        
        String[] sentencas = mSD.sentDetect(texto);
        String[] result_aux = new String[sentencas.length];
        for (int i = 0; i < sentencas.length; i++) {
            String sentenca = sentencas[i];
            String[] res_a = tagger.tag(sentenca.split(" "));
            String res_s = "";
            for (String s : res_a) {
                res_s += s + ",";
            }
            result_aux[i] = res_s + "@!@!" + sentenca;
        }

        Arrays.sort(result_aux);
        String[][] result = new String[sentencas.length][2];
        for (int i = 0; i < result_aux.length; i++) {
            String[] splited = result_aux[i].split("@!@!");
            result[i][0] = splited[0];
            result[i][1] = splited[1];
        }

        return result;
    }

    public static void main(String[] args) throws IOException {
        String texto = "A casa ficou cercada de soldados";
        String[][] result = Tagger.doTagger(texto);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i][0] + "\"" + result[i][1] + "\"");
        }
    }
}